#pragma once

#include "ExtensionEnd.h"

#include "Stereotype.h"

namespace uml {

const int ExtensionEnd::lower_default_ = 0;
ExtensionEnd::ExtensionEnd(const QString& umlType)
    : Property(umlType) {}

void ExtensionEnd::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    Property::fromJson(json, errorList);
    lower_ = json.value("lower").toInt(lower_default_);
    type_ = JsonSerializable::create<Stereotype>(json.value("type"), errorList);

}

void ExtensionEnd::toJson(QJsonObject& json) {
    Property::toJson(json);
    if (lower_ != lower_default_) {
        json.insert("lower", lower_);
    }
    if (type_) {
        QJsonObject type_json;
        type_->toJson(type_json);
        json.insert("type", type_json);
    }
}

/// <summary>
/// lowerBound
    /// The query lowerBound() returns the lower bound of the multiplicity as an
    /// Integer. This is a redefinition of the default lower bound, which normally, for
    /// MultiplicityElements, evaluates to 1 if empty.
/// </summary>
    /// <code>
    /// result = (if lowerValue=null then 0 else lowerValue.integerValue() endif)
    /// </code>
int ExtensionEnd::lowerBound() const
{
    return 0;
}

/// <summary>
/// lower getter
/// This redefinition changes the default multiplicity of association ends, since
/// model elements are usually extended by 0 or 1 instance of the extension
/// stereotype.
/// </summary>
int ExtensionEnd::lower() const {
    return lower_;
}

/// <summary>
/// lower setter
/// This redefinition changes the default multiplicity of association ends, since
/// model elements are usually extended by 0 or 1 instance of the extension
/// stereotype.
/// </summary>
void ExtensionEnd::setLower(int value) {
    lower_ = value;
}

/// <summary>
/// type getter
/// References the type of the ExtensionEnd. Note that this association restricts
/// the possible types of an ExtensionEnd to only be Stereotypes.
/// </summary>
shared_ptr<Stereotype> ExtensionEnd::type() const {
    return type_;
}

/// <summary>
/// type setter
/// References the type of the ExtensionEnd. Note that this association restricts
/// the possible types of an ExtensionEnd to only be Stereotypes.
/// </summary>
void ExtensionEnd::setType(shared_ptr<Stereotype> value) {
    type_ = value;
}

} // namespace uml
